package com.chengfei.spring.cloud.weather.service;

import com.chengfei.spring.cloud.weather.util.XmlBuilder;
import com.chengfei.spring.cloud.weather.vo.City;
import com.chengfei.spring.cloud.weather.vo.CityList;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author chengfei cheng.fei@chinaott.onaliyun.com
 **/
@Service
public class CityDataServieImpl implements CityDataService {
    @Override
    public List<City> listCity() throws Exception {
        //1.读取xml文件
        Resource resource = new ClassPathResource("cityList.xml");
        BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(resource.getInputStream(),"UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = bufferedReader.readLine()) != null){
            buffer.append(line);
        }
        bufferedReader.close();
        //2.xml转为java对象
        CityList cityList = (CityList) XmlBuilder.xmlStrToObject(CityList.class,buffer.toString());
        return cityList.getCityList();
    }
}
